package ewms.controller;

import ewms.common.annotation.Log;
import ewms.common.core.controller.BaseController;
import ewms.common.core.domain.AjaxResult;
import ewms.common.core.page.TableDataInfo;
import ewms.common.enums.BusinessType;
import ewms.common.utils.SecurityUtils;
import ewms.common.utils.poi.ExcelUtil;
import ewms.domain.EwmsWasteDetail;
import ewms.service.IEwmsWasteDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 危废信息信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/wasteDetail")
public class EwmsWasteDetailController extends BaseController {
	@Autowired
	private IEwmsWasteDetailService wasteDetailService;

	/**
	 * 获取危废信息列表
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteDetail:list')")
	@GetMapping("/list")
	public TableDataInfo list(EwmsWasteDetail wasteDetail) {
		return wasteDetailService.selectPageWasteDetailList(wasteDetail);
	}

	@Log(title = "危废信息管理", businessType = BusinessType.EXPORT)
	@PreAuthorize("@ss.hasPermi('system:wasteDetail:export')")
	@GetMapping("/export")
	public AjaxResult export(EwmsWasteDetail wasteDetail) {
		List<EwmsWasteDetail> list = wasteDetailService.selectWasteDetailList(wasteDetail);
		ExcelUtil<EwmsWasteDetail> util = new ExcelUtil<EwmsWasteDetail>(EwmsWasteDetail.class);
		return util.exportExcel(list, "危废信息数据");
	}

	@GetMapping("/importTemplate")
	public AjaxResult importTemplate() {
		ExcelUtil<EwmsWasteDetail> util = new ExcelUtil<EwmsWasteDetail>(EwmsWasteDetail.class);
		return util.importTemplateExcel("危废信息数据");
	}

	/**
	 * 根据危废信息编号获取详细信息
	 */
	@PreAuthorize("@ss.hasPermi('system:user:query')")
	@GetMapping(value = {"/", "/{wasteId}"})
	public AjaxResult getInfo(@PathVariable(value = "wasteId", required = false) Long wasteId) {
		return AjaxResult.success(wasteDetailService.selectWasteById(wasteId));
	}

	/**
	 * 新增危废信息
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteDetail:add')")
	@Log(title = "危废信息管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@Validated @RequestBody EwmsWasteDetail wasteDetail) {
		if (!wasteDetailService.checkWasteCodeUnique(wasteDetail)){
			return AjaxResult.error("危废编码不允许重复");
		}
		if (!wasteDetailService.checkWasteNameUnique(wasteDetail)){
			return AjaxResult.error("危废名称不允许重复");
		}
		return toAjax(wasteDetailService.insertWasteInfo(wasteDetail));
	}

	/**
	 * 修改危废信息
	 */
	@PreAuthorize("@ss.hasPermi('system:wasteDetail:edit')")
	@Log(title = "危废信息管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@Validated @RequestBody EwmsWasteDetail wasteDetail) {
		wasteDetail.setUpdateBy(SecurityUtils.getUsername());
		if (!wasteDetailService.checkWasteCodeUnique(wasteDetail)){
			return AjaxResult.error("危废编码不允许重复");
		}
		if (!wasteDetailService.checkWasteNameUnique(wasteDetail)){
			return AjaxResult.error("危废名称不允许重复");
		}
		return toAjax(wasteDetailService.updateWasteInfo(wasteDetail));
	}

	/**
	 * 删除危废信息
	 */
	@PreAuthorize("@ss.hasPermi('system:user:remove')")
	@Log(title = "危废信息管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{wasteIds}")
	public AjaxResult remove(@PathVariable Long[] wasteIds) {
		return toAjax(wasteDetailService.deleteWasteByIds(wasteIds));
	}
}
